<!-- 
  添加收藏与取消收藏：展商详情
 -->
<template>
<div class="collect" @click.stop="handleCollect" >
    <input type="text" v-model="haNum" style="display:none">
    <span class="btn" :class="isCollect==1?'btn-a':''"></span>
    <span class="txt">{{isCollect==1?$t("collected"):$t("collect-product")}}</span>
</div>
</template>

<script>
import {
    collectProduct,
    cancelCollectProduct
} from "@/api/collect"
export default {
    computed: {
        pid() {
            return this.$route.params.pid || ''
        },
        detail() {
            return this.$store.state.seo.pageContent
        },
    },
    data() {
        return {
            haNum:0,
            isCollect:0
        }
    },
    mounted(){
        this.isCollect=this.detail?this.detail.isCollect:0
    },
    methods: {
        handleCollect() {
            if (!this.userInfo) {
                this.$modal.confirm(this.$t("tip-login"),this.locale).then(() =>{
                    this.go2Login()
                })
                return;
            }
            // 未收藏进行收藏
            if (this.isCollect != 1) {
                this.isCollect=1
                this.handleCollectAdd()
            } else {
                this.handleCollectCancel()
                this.isCollect=0
            }
            this.haNum++
        },
        // 收藏 - 产品
        handleCollectAdd() {
            this.invoke(collectProduct,this.pid).then(res => {
                if (res&&res.code==200) {
                    this.$message.success(this.$t("collect-success"))
                }
            });
        },
        // 收藏 - 产品 - 取消
        handleCollectCancel() {
            this.invoke(cancelCollectProduct, this.pid).then(res => {
                if (res&&res.code==200) {
                    this.$message.info(this.$t("collect-cancel"))
                }
            });
        },
    }
}
</script>
<style lang="scss" scoped src="@/assets/styles/likeCollect.scss"></style>